#include "../base/mempool.h"
#include <assert.h>
#include <stdio.h>


/*	
 * This test case will test the basic functionality of the memory pool
 * including its automatically increased and also the memory leak inside
 * of the memory pool.
 */

void test1() {
	size_t i = 0;
	struct mempool * mp = mempool_create(4,4,1);
	void* pp[1000];
	/*
	 * allocate the memory pool memory
	 */
	printf("The memory pool free size is:%d\n",mempool_get_freesize(mp));
	assert( mempool_get_usesize(mp) == 0 );

	for( i = 0 ; i < 1000; ++i ) {
		pp[i]=mempool_fetch(mp);
	}

	assert( mempool_get_usesize(mp) == 1000 );
	printf("The memory pool free size is:%d\n",mempool_get_freesize(mp));

	for( i = 0 ; i < 1000; ++i ) {
		mempool_return(mp,pp[i]);
	}

	assert( mempool_get_usesize(mp) == 000 );
	printf("The memory pool free size is:%d\n",mempool_get_freesize(mp));

	mempool_destroy(mp);
}


void test2() {
	struct mempool * mp = mempool_create(4,4,1);
	void* pp[1000];
	size_t i;
	size_t li;

	//1.
	li = 100;
	for( i = 0 ; i< li ; ++i ) {
		pp[i] = mempool_fetch(mp);
	}

	//2.
	for( i = 0 ; i<500 ; ++i ) {
		if(i<li)
			mempool_return(mp,pp[i]);
		else
			pp[i-li] = mempool_fetch(mp);
	}
	// 3.
	for( i = 0 ; i < 400 ; ++i ) {
		mempool_return(mp,pp[i]);
	}

	printf("The memory pool free size is:%d\n",mempool_get_freesize(mp));
	mempool_destroy(mp);
}


void test3() {
	struct mempool * mp = mempool_create(4,4,1);
	size_t i ;
	for( i = 0 ; i < 1000; ++i ) {
		mempool_fetch(mp);
	}

	for( i = 0 ; i < 10000 ; ++i ) {
		mempool_return(mp,mempool_fetch(mp));
	}

	printf("The memory pool free size is:%d\n",mempool_get_freesize(mp));
	mempool_destroy(mp);
}


void test() {
	test1();
	test2();
	test3();
}